package com.appbee.cm.client.nav.presenter;

import javax.inject.Inject;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.TreeViewModel;

public class NavTreeViewModel implements TreeViewModel
{
	private final NavControl navControl;
	private final SingleSelectionModel<NavItemWrap> selectionModel = new SingleSelectionModel<NavItemWrap>();
	private Cell<NavItemWrap> cell;


	@Inject
	public NavTreeViewModel(NavControl navControl)
	{
		this.navControl = navControl;
	}

	public void setCell(Cell<NavItemWrap> cell)
	{
		this.cell = cell;
	}

	@Override
	public <T> NodeInfo<?> getNodeInfo(T value)
	{
		if (value == null)
		{
			NavDataProvider dataProvider = navControl.getDataProvider();
			return new DefaultNodeInfo<NavItemWrap>(
dataProvider, cell,
					selectionModel, null);
		}

		if (!(value instanceof NavItemWrap))
			return null;

		NavItemWrap navItem = (NavItemWrap) value;
		NavDataProvider dataProvider = navItem.getNavDataProvider();
		return new DefaultNodeInfo<NavItemWrap>(dataProvider, cell,
				selectionModel, null);
	}

	public SingleSelectionModel<NavItemWrap> getSelectionModel()
	{
		return selectionModel;
	}

	@Override
	public boolean isLeaf(Object value)
	{
		if (value == null)
			return false;
		if (!(value instanceof NavItemWrap))
			return true;

		NavItemWrap navItem = (NavItemWrap) value;
		return navItem.getChildren().isEmpty();
	}

}
